package cn.codeinn.service2.service.impl;

import cn.codeinn.generator.service.impl.ServiceImpl;
import cn.codeinn.generator.util.Page;
import cn.codeinn.service2.entity.Dict;
import cn.codeinn.service2.entity.DictSimpleCategory;
import cn.codeinn.service2.mapper.DictSimpleCategoryMapper;
import cn.codeinn.service2.service.DictService;
import cn.codeinn.service2.service.DictSimpleCategoryService;
import cn.codeinn.service2.vo.DictInfoVO;
import cn.codeinn.service2.vo.DictSimpleCategoryInfoVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("dictSimpleCategoryService")
public class DictSimpleCategoryServiceImpl extends ServiceImpl<DictSimpleCategoryMapper, DictSimpleCategory> implements DictSimpleCategoryService {

    @Autowired
    private DictService dictService;

    @Override
    public Page<DictSimpleCategory> queryPage(Page<DictSimpleCategory> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<DictSimpleCategory> list = mapper.select(page.getCondition(DictSimpleCategory.class));
        page.setPage(new PageInfo<>(list));
        return page;
    }

    @Override
    public Page<DictSimpleCategoryInfoVO> selectSimpleCategory() {
        List<DictSimpleCategoryInfoVO> ret = new ArrayList<>();
        List<DictSimpleCategory> list = mapper.selectList();
        list.stream().map(DictSimpleCategory::getClothid).distinct().forEach(clothId -> {
            DictSimpleCategoryInfoVO clothCategory = new DictSimpleCategoryInfoVO();
            clothCategory.setId(clothId);
            clothCategory.setType("CLOTH");
            clothCategory.setName(dictService.selectById(clothId).getName());
            ret.add(initDictSimpleCategoryInfo(clothCategory, list));
        });
        return new Page<>(ret);
    }

    private DictSimpleCategoryInfoVO initDictSimpleCategoryInfo(DictSimpleCategoryInfoVO parent, List<DictSimpleCategory> list) {
        list.forEach(category -> {
            if ((parent.getType().equals("CLOTH") && category.getParentid().equals(0)) || category.getParentid().equals(parent.getId())) {
                DictSimpleCategoryInfoVO dictSimpleCategoryInfoVO = new DictSimpleCategoryInfoVO();
                dictSimpleCategoryInfoVO.setName(category.getName());
                dictSimpleCategoryInfoVO.setId(category.getId());
                dictSimpleCategoryInfoVO.setType(category.getType());
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(initDictSimpleCategoryInfo(dictSimpleCategoryInfoVO, list));
            }
        });
        return parent;
    }

    @Override
    public Page<DictInfoVO> selectDictInfo(Integer category) {
        DictSimpleCategory dictSimpleCategory = mapper.selectByPrimaryKey(category);
        List<Dict> processList = dictService.selectByCloth(dictSimpleCategory.getClothid());
        List<DictInfoVO> ret = new ArrayList<>();
        processList.stream().filter(dict -> dict.getParentid().equals(dictSimpleCategory.getClothid())).forEach(dict -> {
            DictInfoVO parent = new DictInfoVO();
            parent.setId(dict.getId());
            parent.setName(dict.getName());
            ret.add(findChildren(parent, processList));
        });
        return new Page<>(ret);
    }

    private DictInfoVO findChildren(DictInfoVO parent, List<Dict> list) {
        list.forEach(dict -> {
            if (dict.getParentid().equals(parent.getId())) {
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(findChildren(new DictInfoVO(dict.getId(), dict.getName()), list));
            }
        });
        return parent;
    }
}
